package com.organization.model.services.impl

import org.springframework.stereotype.Service
import com.organization.model.dao.DepartmentDao
import com.organization.model.services.DepartmentService
import org.springframework.beans.factory.annotation.Autowired
import com.organization.model.Department
import com.organization.services.CommonService
import org.hibernate.exception.ConstraintViolationException
import javax.validation.ConstraintViolation
import javax.validation.ValidatorFactory
import javax.validation.Validation
import javax.validation.Validator

/**
 * Created with IntelliJ IDEA.
 * User: inester
 * Date: 09.07.12
 * Time: 12:20
 * To change this template use File | Settings | File Templates.
 */
@Service("departmentService")
class DepartmentServiceImpl implements DepartmentService{

    @Autowired
    DepartmentDao departmentDao
    @Autowired
    CommonService commonService

    //get all field as map or return errors map
    def getDepartmentInfo(depId){
        Department dep = get(commonService.nodeIdToDbId(depId))
        if (dep){
            def result = [name:dep.name, dateCreated:dep.dateCreated, description:dep.description]
            result.putAll(commonService.goodResponse)
            return result
        }
        return commonService.badResponse
    }

    //update department
    def updateDepartmentInfo(params){
        Department dep = get(commonService.nodeIdToDbId(params.nodeId))
        if (dep){
            dep.name = params.name
            dep.dateCreated = commonService.convertDateFromJson(params.dateCreated)
            dep.description = params.description
            //validation
            Set<ConstraintViolation<Department>> constraintViolations = validator.validate(dep);
            if (constraintViolations.size()){
                return commonService.parseConstraintsFromDomain(constraintViolations)
            }
            else{
                update(dep)
                def response = [:]
                response.putAll(commonService.goodResponse)
                response.putAt('newName',params.name)
                response.putAt('nodeId',params.nodeId)
                return response
            }
        }
        return commonService.badResponse
    }

    //add new department
    def addDepartment(params){
        Department dep = new Department(
                name: params.name,
                dateCreated: commonService.convertDateFromJson(params.dateCreated),
                description: params.description
        );
        if (dep){
            //validation
            Set<ConstraintViolation<Department>> constraintViolations = validator.validate(dep);
            if (constraintViolations.size()){
                return commonService.parseConstraintsFromDomain(constraintViolations)
            }
            else {
                save(dep)
                def response = [:]
                response.putAll(commonService.goodResponse)
                response.putAt('nodeId', commonService.depNodeStr + dep.id.toString())
                response.putAt('parentNodeId', params.parentNodeId)
                return response
            }
        }
        return commonService.badResponse
    }

    //delete existing department
    def deleteDepartment(params){
        Department dep = get(commonService.nodeIdToDbId(params.nodeId))
        if (dep){
            delete(dep)
            return commonService.goodResponse
        }
        return commonService.badResponse;
    }

    @Override
    public void save(Department dep){
        departmentDao.save(dep)
    }

    @Override
    public void update(Department dep){
        departmentDao.update(dep)
    }

    @Override
    public void delete(Department dep){
        departmentDao.delete(dep)
    }

    @Override
    public List<Department> departmentList() {
        departmentDao.departmentList()
    }

    @Override
    public Department get(int id) {
        departmentDao.get(id)
    }

    //validator
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
}